#include <getopt.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#define LINESIZE 2048 

char* const short_options="r:";
struct option long_options[] = {
	{"read",1,NULL,'r'},
	{0,0,0,0},
};

typedef enum ntyaction{
	CREATE,
	DELETE,
	MODIFY,
	MOVEFROM,
	MOVETO,
	RENAME,
}NTFACTION;

typedef enum ntynodekind{
	NTFFILE,
	NTFFOLDER,
}NODEKIND;

typedef struct ntyelement {
	char *name;
	char *path;
	NODEKIND kind;
	NTFACTION action;
}NTFELEMENT;

typedef NTFELEMENT* PNTFELEMENT; 

int readline(char *line, char **token);
PNTFELEMENT readFile(char *filepath);
NTFACTION getAction(char *actionname);
void usage();
int nflag;

int main(int argc, char **argv)
{
	int ch, moveflag=0;
	char *filepath = NULL;
	PNTFELEMENT pelement, pmovefrom,tmpelement;

	while((ch = getopt_long(argc, argv, short_options, long_options, NULL)) != -1){
		switch (ch) {
			case 'r': //read
				fprintf(stderr, "Get path: %s\n", optarg);
				
				if(optarg == NULL) {
					fprintf(stderr, "Please input path\n");
					exit(EXIT_FAILURE);
				}
	
				nflag=1;
				if(pelement = readFile(optarg)){
					fprintf(stderr, "name:%s path: %s kind: %d action: %d\n", pelement->name, pelement->path, pelement->kind, pelement->action);
/*                    config_set("notify_name",pelement->name);
                    char tmp_path[1024];
                    int len=strlen(pelement->path);
                    strncpy(tmp_path,pelement->path,len-1);
                    config_set("notify_path",tmp_path);
                    if(pelement->kind==NTFFILE)
                    config_set("notify_kind","0");
                    else
                    config_set("notify_kind","1");
                    if(pelement->action==CREATE ||(pelement->action==MOVETO))
                    config_set("notify_action","0");
                    if(pelement->action==DELETE || (pelement->action==MOVEFROM))
                    config_set("notify_action","1");
					*/
					//save acting file, when backup file need to save action, when network ok,conitune
                    if(pelement->action==MODIFY)
					{
						nflag=0;
						while(tmpelement = readFile(optarg)){
							fprintf(stderr,"---------------------------------read file------\n");
							fprintf(stderr, "old name:%s path: %s kind: %d action: %d\n", pelement->name, pelement->path, pelement->kind, pelement->action);
							fprintf(stderr, "new name:%s path: %s kind: %d action: %d\n",
									tmpelement->name, tmpelement->path, tmpelement->kind, tmpelement->action);
							//if (tmpelement->action==MODIFY && (strcmp(tmpelement->name,pelement->name)==0) && 
							//		(strcmp(tmpelement->name,pelement->path)==0) )
							if (tmpelement->action==MODIFY && (strcmp(tmpelement->name,pelement->name)==0) && 
									(strcmp(tmpelement->path,pelement->path)==0) && (tmpelement->kind==pelement->kind) )
								continue;
							else
								break;
						}
					}
					//config_set("notify_action","2");

				}
				free(pelement);
				break;
			default:
				fprintf(stderr, "Invalid option\n");
				usage();
				exit(EXIT_FAILURE);
		}
	}

}

int readline(char *line, char **token)
{
	char *p,*q;
	int i=0;
	int t=0;

    fprintf(stderr, "READ LINE:%s\n", line);
	if(nflag==1)
	{
		char cmd[256];
		//sprintf(cmd,"echo %s > now_line",line);
		sprintf(cmd,"echo \"%s\" > now_line",line);
		system(cmd);
	}
	for(i=0; i<3; i++)
	{
		if(!i){
			//p = strtok(line, " ,");
			p = strtok(line, "|");
		fprintf(stderr,"11 p is %s\n",p);
			if(p){
				token[i] = p;
			}else
				return -1;
		}else{
			//p = strtok(line, " ,");
			p = strtok(NULL, "|");
		fprintf(stderr,"22 p is %s\n",p);
			if(p){
				token[i] = p;
			}
			/*
			else if(i == 3){
				token[i] = NULL;
				return 0;
			}else
				return -1;
				*/
		}
		fprintf(stderr,"token[%d] is %s\n",i,token[i]);
	}
	fprintf(stderr,"---------------------\n");
	fprintf(stderr,"token[1] is %s\n",token[1]);
	q=strtok(token[1], ",");
	if(q)
	{
		fprintf(stderr," q is %s\n",q);
		token[1]=q;
	}
	q=strtok(NULL,",");
	if(q)
	{
		fprintf(stderr," this q is %s\n",q);
		token[3]=q;
		fprintf(stderr," token[3] is %s\n",token[3]);
	}
	return 0;
}

NTFACTION getAction(char *actionname)
{
	if(!strcmp(actionname, "CREATE"))
		return CREATE;
	else if(!strcmp(actionname, "DELETE"))
		return DELETE;
	else if(!strcmp(actionname, "MODIFY"))
		return MODIFY;
	else if(!strcmp(actionname, "MOVED_FROM"))
		return MOVEFROM;
	else if(!strcmp(actionname, "MOVED_TO"))
		return MOVETO;
}

PNTFELEMENT readFile(char *filepath)
{
	FILE *fd, *tmpfd;
	PNTFELEMENT pele;
	char line[LINESIZE], otherline[LINESIZE], *tmp;
	char **token;
	FILE *fd1;
	char line1[LINESIZE];
	if(nflag==0)
	{
		fprintf(stderr,"-----------------flag is %d-----------------\n",nflag);
		fd1 = fopen("now_line", "rb");
		if(fd1 == NULL){
			fprintf(stderr, "Failed to open file");
			return NULL;
		}
		if(fgets(line1, LINESIZE, fd1))
			fprintf(stderr,"-----line1 is %s\n",line1);
		}
	fd = fopen(filepath, "rb");
	if(fd == NULL){
		fprintf(stderr, "Failed to open file");
		return NULL;
	}

	if(fgets(line, LINESIZE, fd)){
		if(nflag==0)
		{
			fprintf(stderr,"-----line2 is %s\n",line);
			if(strcmp(line1,line) !=0) return NULL;
		}
		if(line[0] == '\n') return NULL;
		//drop '\n'
		if(tmp = strstr(line, "\n"))
			*tmp = '\0';

		token = (char **)malloc(4 * sizeof(char *));
		if(readline(line, token) == -1){
			fclose(fd);
			free(token);
			return NULL;
		}
		
		pele = (PNTFELEMENT)malloc(sizeof(NTFELEMENT));

		pele->path = token[0];
		pele->action = getAction(token[1]);
		fprintf(stderr,"----token[3] is %s\n",token[3]);	
		if(token[3] != NULL && (strcmp(token[3], "ISDIR")==0)) //if is folder
		{
			pele->kind = NTFFOLDER;
			//pele->name = token[3];
			pele->name = token[2];
			fprintf(stderr, "FOLDER: name:%s path: %s kind: %d action: %d\n", pele->name, pele->path, pele->kind, pele->action);
		}
		else
		{
			pele->kind = NTFFILE;
			pele->name = token[2];
			fprintf(stderr, "File: name:%s path: %s kind: %d action: %d\n", pele->name, pele->path, pele->kind, pele->action);
		}
		
		//copy file to tmp_even except first line
		tmpfd = fopen("tmp_even", "wb");
		while(fgets(otherline, LINESIZE, fd)){
			fwrite(otherline, sizeof(char), strlen(otherline), tmpfd);
			memset(otherline, 0, LINESIZE);
		}

		free(token);
		fclose(tmpfd);	
		fclose(fd);
		
		//copy tmp_even to file
		char cmd[256];
		sprintf(cmd, "cp tmp_even %s", filepath);
		system(cmd);
		return pele; 
	}else{
		fclose(fd);
		return NULL;
	}
}

void usage()
{
	int i=0;
	fprintf(stderr, "usage:");
	for(i=0; long_options[i].val != 0;i++)
		fprintf(stderr, "-%c --%s", long_options[i].val, long_options[i].name);
}
